﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Cuttingstylussolid
    {
        public static void _Cuttingstylussolid(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 2; //放大系数


            double oal = 29.97;
            double lpr = 21.45;
            double dconms = 6.95;
            double dn = 6.25;
            double ls = 10.54;
            double dcx = 7.07;
            double ampx = 3.59;
            double kapr = 46;


            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dconms / 2 - 0.5);
            Vector2 v5 = new Vector2(v3.X + 0.5, v3.Y + dconms / 2);
            Vector2 v6 = new Vector2(v3.X + ls, v5.Y);
            Vector2 v7 = new Vector2(v6.X + 0.8, v3.Y + dn / 2);
            Vector2 v8 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v9 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v10 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v11 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v12 = new Vector2(v7.X + lpr / 5.66, v7.Y);
            Vector2 v13 = new Vector2(v12.X + 0.5, v3.Y + dcx / 2);
            Vector2 v14 = new Vector2(v13.X, v13.Y * -1);
            Vector2 v15 = new Vector2(v12.X, v12.Y * -1);
            Vector2 v16 = new Vector2(v3.X + oal, v3.Y);
            Vector2 v17 = new Vector2(v16.X - dcx / 2 * Math.Tan(kapr * pi / 180), v14.Y);
            Vector2 v18 = new Vector2(v17.X, v16.Y + dcx / 3.415);
            Vector2 v19 = new Vector2(v18.X, v18.Y - dcx / 2.571);
            Vector2 v20 = new Vector2(v17.X, v17.Y + dcx / 8.126);
            Vector2 v21 = new Vector2(v17.X - lpr / 15.432, v17.Y);
            Vector2 v22 = new Vector2(v21.X, v21.Y + dcx / 12.19);
            Vector2 v23 = new Vector2(v21.X, v21.Y + dcx / 2.74);
            Vector2 v24 = new Vector2(v23.X, v23.Y + dcx / 2.561);
            Vector2 v25 = new Vector2(v24.X, v24.Y + dcx / 5.662);
            Vector2 v26 = new Vector2(v25.X, v13.Y);
            Vector2 v27 = new Vector2(v17.X, v13.Y);
            Vector2 v45 = new Vector2(v16.X - lpr, v16.Y);
            Vector2 v46 = new Vector2(v45.X, v45.Y + dconms / 2 + 1);
            Vector2 v47 = new Vector2(v46.X, v46.Y * -1);

            Vector2 v28 = new Vector2(v2.X - oal / 2.974, v2.Y - oal / 1.277);
            Vector2 v29 = new Vector2(v28.X - lpr / 2.529, v28.Y);
            Vector2 v30 = new Vector2(v28.X - lpr / 9.449, v28.Y + dcx / 6.607);
            Vector2 v31 = new Vector2(v30.X, v28.Y - dcx / 6.607);
            Vector2 v40 = new Vector2(v30.X - lpr / 8.614, v30.Y + dcx / 2.618);
            Vector2 v32 = new Vector2(v31.X - lpr / 8.755, v31.Y - dcx / 3.142);
            Vector2 v33 = new Vector2(v32.X - lpr / 34.048, v32.Y + dcx / 7.855);
            Vector2 v34 = new Vector2(v32.X - lpr / 23.833, v32.Y + dcx / 4.017);
            Vector2 v35 = new Vector2(v32.X - lpr / 17.024, v32.Y + dcx / 2.438);
            Vector2 v36 = new Vector2(v32.X - lpr / 14.692, v32.Y + dcx / 1.733);
            Vector2 v37 = new Vector2(v32.X - lpr / 15.657, v32.Y + dcx / 1.514);
            Vector2 v38 = new Vector2(v32.X - lpr / 18.816, v32.Y + dcx / 1.365);
            Vector2 v39 = new Vector2(v40.X - lpr / 32.015, v40.Y - dcx / 12.19);
            Vector2 v41 = new Vector2(v28.X - lpr / 2.018, v28.Y + lpr / 3.346);
            Vector2 v42 = new Vector2(v41.X + lpr / 1.267, v41.Y);
            Vector2 v43 = new Vector2(v42.X, v42.Y - lpr / 1.421);
            Vector2 v44 = new Vector2(v41.X, v43.Y);
            Vector2 v48 = new Vector2(v28.X - lpr / 9.449, v28.Y);



            //作图
            //1
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            Line line15 = new Line(v46, v47);
            line15.Layer = centre;
            line15.Linetype = Linetype.DashDot;
            line15.LinetypeScale = 3;
            dxf.AddEntity(line15);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            fun.PolyFill(dxf, line2, 0.1, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v6));
            line3.Vertexes.Add(new LwPolylineVertex(v7));
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            line3.Vertexes.Add(new LwPolylineVertex(v9));
            fun.PolyFill(dxf, line3, 0.1, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v12));
            line4.Vertexes.Add(new LwPolylineVertex(v15));
            line4.Vertexes.Add(new LwPolylineVertex(v14));
            line4.Vertexes.Add(new LwPolylineVertex(v13));
            line4.Vertexes.Add(new LwPolylineVertex(v12));
            line4.Vertexes.Add(new LwPolylineVertex(v7));
            line4.Vertexes.Add(new LwPolylineVertex(v8));
            line4.Vertexes.Add(new LwPolylineVertex(v15));
            fun.PolyFill(dxf, line4, 0.1, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v13));
            line5.Vertexes.Add(new LwPolylineVertex(v27));
            line5.Vertexes.Add(new LwPolylineVertex(v16));
            line5.Vertexes.Add(new LwPolylineVertex(v17));
            line5.Vertexes.Add(new LwPolylineVertex(v14));
            fun.PolyFill(dxf, line5, 0.1, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v20));
            line6.Vertexes.Add(new LwPolylineVertex(v17));
            line6.Vertexes.Add(new LwPolylineVertex(v21));
            line6.Vertexes.Add(new LwPolylineVertex(v22));
            line6.Vertexes.Add(new LwPolylineVertex(v20));
            line6.Vertexes.Add(new LwPolylineVertex(v16));
            line6.Vertexes.Add(new LwPolylineVertex(v19));
            line6.Vertexes.Add(new LwPolylineVertex(v23));
            line6.Vertexes.Add(new LwPolylineVertex(v22));
            line6.Vertexes[7].Bulge = Math.Tan(37 * pi / 180 / 4);
            fun.PolyFill(dxf, line6, 0.1, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v19));
            line7.Vertexes.Add(new LwPolylineVertex(v18));
            line7.Vertexes.Add(new LwPolylineVertex(v24));
            line7.Vertexes.Add(new LwPolylineVertex(v23));
            line7.Vertexes.Add(new LwPolylineVertex(v19));
            line7.Vertexes.Add(new LwPolylineVertex(v16));
            line7.Vertexes.Add(new LwPolylineVertex(v18));
            fun.PolyFill(dxf, line7, 0.1, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v26));
            line8.Vertexes.Add(new LwPolylineVertex(v25));
            line8.Vertexes.Add(new LwPolylineVertex(v27));
            line8.Vertexes.Add(new LwPolylineVertex(v25));
            line8.Vertexes.Add(new LwPolylineVertex(v24));
            line8.Vertexes[3].Bulge = Math.Tan(19 * pi / 180 / 4);
            fun.PolyFill(dxf, line8, 0.1, contour);

            //2
            Line line9 = new Line(v28, v29);
            line9.Layer = centre;
            line9.Linetype = Linetype.DashDot;
            line9.LinetypeScale = 3;
            dxf.AddEntity(line9);

            LwPolyline line10 = new LwPolyline();
            line10.Vertexes.Add(new LwPolylineVertex(v39));
            line10.Vertexes.Add(new LwPolylineVertex(v40));
            line10.Vertexes.Add(new LwPolylineVertex(v30));
            line10.Vertexes.Add(new LwPolylineVertex(v31));
            line10.Vertexes.Add(new LwPolylineVertex(v32));
            line10.Vertexes.Add(new LwPolylineVertex(v33));
            line10.Vertexes.Add(new LwPolylineVertex(v34));
            line10.Vertexes.Add(new LwPolylineVertex(v35));
            line10.Vertexes.Add(new LwPolylineVertex(v36));
            line10.Vertexes.Add(new LwPolylineVertex(v37));
            line10.Vertexes.Add(new LwPolylineVertex(v38));
            line10.Vertexes.Add(new LwPolylineVertex(v39));
            line10.Vertexes[2].Bulge = Math.Tan(-85 * pi / 180 / 4);
            line10.Vertexes[4].Bulge = Math.Tan(-85 * pi / 180 / 4);
            line10.Vertexes[5].Bulge = Math.Tan(72 * pi / 180 / 4);
            line10.Vertexes[6].Bulge = Math.Tan(-72 * pi / 180 / 4);
            line10.Vertexes[7].Bulge = Math.Tan(76 * pi / 180 / 4);
            line10.Vertexes[8].Bulge = Math.Tan(-87 * pi / 180 / 4);
            line10.Vertexes[9].Bulge = Math.Tan(68 * pi / 180 / 4);
            line10.Vertexes[10].Bulge = Math.Tan(-81 * pi / 180 / 4);
            fun.PolyFill(dxf, line10, 0.1, contour);

            Line line11 = new Line(v41, v42);
            line11.Layer = refin;
            line11.Linetype = Linetype.Dashed;
            line11.LinetypeScale = 5;
            dxf.AddEntity(line11);

            Line line12 = new Line(v42, v43);
            line12.Layer = refin;
            line12.Linetype = Linetype.Dashed;
            line12.LinetypeScale = 5;
            dxf.AddEntity(line12);

            Line line13 = new Line(v43, v44);
            line13.Layer = refin;
            line13.Linetype = Linetype.Dashed;
            line13.LinetypeScale = 5;
            dxf.AddEntity(line13);

            Line line14 = new Line(v41, v44);
            line14.Layer = refin;
            line14.Linetype = Linetype.Dashed;
            line14.LinetypeScale = 5;
            dxf.AddEntity(line14);


            //坐标系
            fun.Axis(dxf, v3, -90, 180, "", "", "", 0.3, 1, 0);

            fun.Axis(dxf, v45, -90, 180, "", "", "", 0.3, 1, 0);

            fun.Axis(dxf, v16, -90, 180, "", "", "", 0.3, 1, 0);

            fun.Axis(dxf, v48, -90, 180, "", "", "", 0.3, 1, 0);



            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;

            //1
            fun.dimrot1(dxf, v4, v16, 0, oal / 2, 1, height, arrowsize, exline, "OAL,LF", "", "");

            fun.dimrot1(dxf, v46, v16, 0, oal / 3, 1, height, arrowsize, exline, "LPR,LTA", "", "");

            fun.dimrot1(dxf, v5, v10, 90, dconms / 2, 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.dimrot1(dxf, v27, v17, -90, dconms * 3 / 2, 1, height, arrowsize, exline, "DCX", "", "");

            fun.dimrot1(dxf, v7, v8, -90, dconms / 3, 1, height, arrowsize, exline, "DN", "", "");

            fun.dimrot1(dxf, v11, v9, 180, dconms * 2 / 3, 1, height, arrowsize, exline, "LS", "", "");

            fun.dimrot1(dxf, v17, v16, 180, dconms * 2 / 3 + (v17.Y + v16.Y) / 2 - (v9.Y + v11.Y) / 2, 1, height, arrowsize, exline, "APMX", "", "");
             
            fun.jdbz2(dxf, new Line(v18, fun.Polar(v18, 150 * pi / 180, 0.1)), new Line(v16, fun.Polar(v16, 180 * pi / 180, 0.1)), dn * 2, height, arrowsize, exline, "FHA", "", "");

            fun.jdbz2(dxf, new Line(v27, fun.Polar(v27, (90 + kapr) * pi / 180, 0.1)), new Line(v17, fun.Polar(v17, (-90 - kapr) * pi / 180, 0.1)), dn * 1.5, height, arrowsize, exline, "PRFA", "", "");

            //2
            fun.dimrot1(dxf, v30, v31, -90, lpr / 4, 1, height, arrowsize, exline, "DC", "", "");


            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}